const app = getApp();
import HttpRequst from '../../../utils/httpRequst.js'
import API from '../../../utils/config.js'
import SendSMS from '../../../utils/sendSMS.js'
import SendFormId from '../../../utils/sendFormId.js'

Page({
  data: {
    inputData: {
      input_value: "", //输入框的初始内容
      value_length:0, //输入框密码位数
      isNext: true, //是否有下一步的按钮
      get_focus: false, //输入框的聚焦状态
      focus_class: false, //输入框聚焦样式
      value_num: [1, 2, 3, 4, 5, 6], //输入框格子数
      height: "84rpx", //输入框高度
      width: "636rpx", //输入框宽度
      see: false, //是否明文展示
      interval: false, //是否显示间隔格子
    },

    
    isRealInputFocus:false,
    submitting: false,
    isFocus: false,
    pwdVal: "",
    pwdDialogIsShow: false,
    shopInfo: "",
    useInfo:"",
    yzmVal: "",
    yzmRemainTime: 60,
    telphone:"",
    telphoneHide:"",
    isYzmInputing: false, //是否在规定输入验证码的时间内
    totalM: "", //订单金额
    reduceM: "", //减免金额
    realM: "", //

    biz_id: "" //商家id

  },
  onLoad: function(options) {
    var userInfo = app.getStoreUserInfo();
    
   
    var that = this;
    //根据当前的商家，获取数据
    this.setData({
      biz_id: options.biz_id,
      useInfo: userInfo,
      telphone: userInfo.mobile,
      telphoneHide: userInfo.mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')

    });
    HttpRequst(true, API.prepay, 1, app.getUkey(),options, "POST", false, function(res) {
      
      if (!res.status) {
        wx.showToast({
          title: '请求出错了！',
          icon: 'none',
          duration: 2000
        })
      } else {

        that.setData({
          shopInfo: res.data
        })
      }
    })
  },

  realInputFoucs:function(){
    this.setData({
     
      ["inputData.get_focus"]: true,
      ["inputData.focus_class"]: true
    })
    
  },
  realInputPwd: function (e){
   
    this.setData({

      ["inputData.input_value"]: e.detail.value,
      ["inputData.value_length"]: e.detail.value.length 
    })
   
   
    if(e.detail.value.length==6){
      this.setData({
        isRealInputFocus:false
      })
    }
    
    
  },

 

  formSubmit: function(e) {
    
    var that = this;
    const params = e.detail.value;
    params.pwd = this.data.pwdVal; //明文加密
    params.biz_id = this.data.biz_id; //明文加密
    if (this.data.shopInfo.discounts.length){
      params.dis_id = this.data.shopInfo.discounts[0].dis_id;
    }
    
    
    this.setData({
      submitting: true,

    })
    app.getFormIds(e.detail.formId);


    //HttpRequst(loading,url,发送header类型, sessionId, params,method, ask, function (res){})
    HttpRequst(true, API.pay, 1, app.getUkey(), params, "POST", false, function(res) {
      that.setData({
        submitting: false
      });

      if (!res.status) {
        wx.showModal({
          title: '提示',
          content: res.msg,
          showCancel: false
        })
      } else {
        
        SendFormId(app, "POST",2, "");
        that.setData({
          pwdDialogIsShow: false
        });
        var queryStr = "biz_id=" + that.data.biz_id + "&money=" + res.data.money + "&discount=" + res.data.discount + "&biz_name=" + res.data.biz_name + "&realpay=" + res.data.realpay
        wx.navigateTo({
          url: '/pages/index/payresult/payresult?' + queryStr
        });
      }


    });
  },
  //
  valueSix(e) {
    this.setData({
      pwdVal: e.detail
    })

  },
  valueNow(e) {
    this.setData({
      pwdVal: e.detail
    })
  },
  //获取总的金额
  getTotalMoney: function(e) {

    var reduceM = "",
      realM = "",
      totalM = "";
    if (e.detail.value.split(".")[1] && e.detail.value.split(".")[1].length > 2) {
      totalM = Number(e.detail.value).toFixed(2);
    } else {
      totalM = e.detail.value;
    }


    if (this.data.shopInfo.discounts.length) {
      //没有门槛  金额直接计算泽口
      if (this.data.shopInfo.discounts[0].stype==0){
       
        if (this.data.shopInfo.discounts[0].dtype == 0) {
          realM = totalM - this.data.shopInfo.discounts[0].discount;
          reduceM = this.data.shopInfo.discounts[0].discount;
        } else if (this.data.shopInfo.discounts[0].dtype == 1) {
          realM = (totalM * this.data.shopInfo.discounts[0].discount / 10).toFixed(2);
          reduceM = (totalM - realM).toFixed(2);
        }
      }else{
        if (this.data.shopInfo.discounts[0].dtype == 0) {
          //满减
          if (totalM >= Number(this.data.shopInfo.discounts[0].start)) {
            realM = totalM - this.data.shopInfo.discounts[0].discount;
            reduceM = this.data.shopInfo.discounts[0].discount;
          } else {
            realM = totalM;
            reduceM = "";
          }

        } else if (this.data.shopInfo.discounts[0].dtype == 1) {
          //折扣
          if (totalM >= Number(this.data.shopInfo.discounts[0].start)) {
            realM = (totalM * this.data.shopInfo.discounts[0].discount / 10).toFixed(2);
            reduceM = (totalM - realM).toFixed(2);
          } else {
            realM = totalM;
            reduceM = "";
          }
        }
      }
     
    }else{
      realM = totalM;
    }
    this.setData({
      totalM: totalM,
      reduceM: reduceM,
      realM: realM > 0 ? realM :0
    });
  },
  //点击立即买单=>后端请求数据=》根据返回是否设置了支付信息进行显示
  //传给后端(用户唯一标识)
  openPayDialog: function() {
    var that=this;
    if (this.data.realM > this.data.shopInfo.left_money){
      wx.showModal({
        title: '提示',
        content: '本次支付金额超过了您的账户余额,请修改金额后重新支付',
        showCancel: false,
        confirmText: "我知道了"
      })
      return false;
    }
    HttpRequst(true, API.home, 1, app.getUkey(),"", "POST", false, function (res) {
      if (!!that.data.realM && (that.data.realM > (res.data.account.money * 0.5))) {
        wx.showModal({
          title: '提示',
          content: '本次支付金额不能超过总金额的50%，请修改金额后重新支付',
          showCancel: false,
          confirmText: "我知道了"
        })
      }else{
        that.setData({
          pwdDialogIsShow: true,
          isRealInputFocus:true
        })
      }
    })
  },
  //关闭支付弹出框
  closePayDialog: function(e) {
    this.setData({
      pwdDialogIsShow: false
    })
  },
  //输入验证码的时候实时获取数据
  inputYzmCode: function(e) {
    this.setData({
      yzmVal: e.detail.value
    });

  },
  //获取验证码
  getYzmCode: function() {

    var that = this;
    //验证手机
    if (this.data.telphone && /^1[23456789]\d{9}$/.test(this.data.telphone)) {
      //验证通过，服务端发送请求
      SendSMS({
        mobile: this.data.telphone
      }, "POST", false, 2, function(res) {
        

        if (!res.status) {
          wx.showToast({
            title: '获取失败',
            icon: 'none',
            duration: 2000
          })
        } else {
          //服务端发送短信成功
          that.setData({
            isYzmInputing: true,
            yzmVal: ""
          })
          that._countdownYzm();
        }

      });
    }



  },
  _countdownYzm: function() {
    var that = this;
    var Timer = setInterval(function() {
      if (that.data.yzmRemainTime <= 1) {
        clearInterval(Timer);
        that.setData({
          yzmRemainTime: 60,
          isYzmInputing: false
        });
      };
      that.setData({
        yzmRemainTime: that.data.yzmRemainTime - 1
      });
    }, 1000)
  },

})